﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Json;
using WorkSpace.UI.WorkSpaceRef;
using System.IO;
using System.Windows.Media.Imaging;
using SmartSpace.UI.Silverlight;

namespace WorkSpace.UI.Windows
{
    public partial class LinkWindow : ChildWindow
    {
        private Link _appLink;

        public LinkWindow(Link appLink, string category)
        {
            InitializeComponent();

            _ctlEditCategory.Text = category;
            _appLink = appLink;
            this.DataContext = _appLink;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.SaveLinkCompleted += new EventHandler<SaveLinkCompletedEventArgs>(client_SaveLinkCompleted);
            client.SaveLinkAsync(_appLink);
        }

        void client_SaveLinkCompleted(object sender, SaveLinkCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show("添加失败!");
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void _ctlBtnUpload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "图片(*.png)|*.png";
            if (dlg.ShowDialog() == true)
            {
                IconData icon = new IconData();
                icon.Id = Guid.NewGuid();
                icon.Caption = dlg.File.Name;

                using (Stream stream = dlg.File.OpenRead())
                {
                    icon.Data = new byte[(int)stream.Length];
                    stream.Seek(0, SeekOrigin.Begin);

                    stream.Read(icon.Data, 0, icon.Data.Length);
                }

                WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
                client.SaveIconCompleted += new EventHandler<SaveIconCompletedEventArgs>(client_SaveIconCompleted);
                client.SaveIconAsync(icon, icon);
            }
        }

        void client_SaveIconCompleted(object sender, SaveIconCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                IconData icon = e.UserState as IconData;
                _ctlEditIconUrl.Text = "./Handlers/WorkSpaceLoadIconHandler.ashx?id=" + icon.Id.ToString("D");
                _ctlIcon.Source = new BitmapImage(new Uri(_ctlEditIconUrl.Text, UriKind.RelativeOrAbsolute));
            }
        }
    }
}

